package tp.disenio.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

//CU-002 Autenticar Usuario

public class Ingreso extends Composite{
	
	private Button btnOkIngreso;
	private Button btnVerCompetenciasDeportivas;
	private Button btnRegistro;
	private TextBox txtBxCorreoElectronico;
	private PasswordTextBox txtBxPassword;
	private final RootPanel rootPanel;
	
	public Ingreso(){
		
		//Root Panel
		rootPanel = RootPanel.get("nameFieldContainer");
		RootPanel.get().getElement().getStyle().setPosition(Position.RELATIVE);
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("346px", "449px");

		// WIDGETS
		
		btnRegistro = new Button("Registro");
		absolutePanel.add(btnRegistro, 125, 355);
		btnRegistro.setSize("70", "30");
		
		btnOkIngreso = new Button("Ok");
		absolutePanel.add(btnOkIngreso, 120, 165);
		
		btnVerCompetenciasDeportivas = new Button("Ver Competencias Deportivas");
		absolutePanel.add(btnVerCompetenciasDeportivas, 70, 285);
		btnVerCompetenciasDeportivas.setSize("200", "30");
		
		txtBxCorreoElectronico = new TextBox();
		txtBxCorreoElectronico.setStyleName("TextBox");
		absolutePanel.add(txtBxCorreoElectronico, 120, 95);
		txtBxCorreoElectronico.setSize("150px", "20px");
				
		txtBxPassword = new PasswordTextBox();
		txtBxPassword.setStyleName("TextBox");
		absolutePanel.add(txtBxPassword, 120, 130);
		txtBxPassword.setSize("150px", "20px");
		
		
		// LABELS
		
		Label lblUsuariosNoRegistrados = new Label("Usuarios No Registrados");
		lblUsuariosNoRegistrados.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		lblUsuariosNoRegistrados.setStyleName("subTitulo");
		absolutePanel.add(lblUsuariosNoRegistrados, 20, 238);
				
		Label lblregistrateGratis = new Label("\u00A1\u00A1\u00A1Registrate Gratis!!!");
		lblregistrateGratis.setStyleName("expresionLabel");
		absolutePanel.add(lblregistrateGratis, 75, 321);
				
		Label lblEmail = new Label("E-mail");
		lblEmail.setStyleName("label");
		absolutePanel.add(lblEmail, 38, 93);
		lblEmail.setSize("62px", "18px");
		
		Label lblPassword = new Label("Password");
		lblPassword.setStyleName("label");
		absolutePanel.add(lblPassword, 38, 137);
		
		Label lblUsuariosRegistrados = new Label("Usuarios Registrados");
		lblUsuariosRegistrados.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		lblUsuariosRegistrados.setStyleName("subTitulo");
		absolutePanel.add(lblUsuariosRegistrados, 40,20);
		
		Label lblIdentificarme = new Label("Identificarme");
		lblIdentificarme.setStyleName("expresionLabel");
		absolutePanel.add(lblIdentificarme, 121, 55);
		
		
			
	    // HANDLERS
			
		getTxtBxPassword().addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
			
				if(event.getUnicodeCharCode()==13){
					getBtnOk().click();
					
				}
			
			}
		});
		
		getTxtBxCorreoElectronico().addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
			
				if(event.getUnicodeCharCode()==13){
					getBtnOk().click();
				}
			}
		});

		getBtnOk().addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {

			final LoginServiceAsync loginService = GWT.create(LoginService.class);
			final DialogBox dialogBox = new DialogBox();
			final Button dialogBoxButton = new Button("Ok");
			final Label textToServerLabel = new Label();
			final HTML serverResponseLabel = new HTML();
			
			dialogBoxButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					dialogBox.hide();
					getTxtBxCorreoElectronico().setFocus(true);
				}
			});
			
			dialogBoxButton.addKeyPressHandler(new KeyPressHandler() {
				
				@Override
				public void onKeyPress(KeyPressEvent event) {
				
					if(event.getUnicodeCharCode()==13){
						dialogBox.hide();
						getTxtBxCorreoElectronico().setFocus(true);
					}
				}
			});
			
			
			 AsyncCallback<String> callback = new AsyncCallback<String>() {
				 
				     public void onFailure(Throwable caught) {
				      				    
				    	 // Show DialogBox
				    	 
				    	 	dialogBox.setText("Auslober");
							dialogBox.setAnimationEnabled(true);
							VerticalPanel dialogVPanel = new VerticalPanel();
							dialogVPanel.addStyleName("dialogVPanel");
							dialogVPanel.add(new HTML("<b>Respuesta del servidor:</b>"));
							dialogVPanel.add(textToServerLabel);
							dialogVPanel.add(new HTML("<br>"+(String)caught.getMessage()));
							dialogVPanel.add(serverResponseLabel);
							dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
							dialogVPanel.add(dialogBoxButton);
							dialogBox.setWidget(dialogVPanel);
							dialogVPanel.setSize("250px", "100px");
							dialogBox.setPopupPosition(535,225);
							dialogBox.show();

							dialogBoxButton.setFocus(true);
				    	
				    }

					@Override
					public void onSuccess(String result) {
												
						// Show DialogBox
						
						dialogBox.setText("Auslober");
						dialogBox.setAnimationEnabled(true);
						VerticalPanel dialogVPanel = new VerticalPanel();
						dialogVPanel.addStyleName("dialogVPanel");
						dialogVPanel.add(new HTML("<b>Respuesta del servidor:</b>"));
						dialogVPanel.add(textToServerLabel);
						dialogVPanel.add(new HTML("<br>"+result));
						dialogVPanel.add(serverResponseLabel);
						dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
						dialogVPanel.add(dialogBoxButton);
						dialogBox.setWidget(dialogVPanel);
						dialogVPanel.setSize("250px", "100px");
						dialogBox.setPopupPosition(535,225);
						dialogBox.show();
						
					// Show a HomeUA interface
	
//						setVisibilidad(false);
						rootPanel.remove(rootPanel.getWidget(1));
						rootPanel.add(new HomeUA(getTxtBxCorreoElectronico().getText()),100,50);
					 
					// Set Focus to dialogBoxButton
						
						dialogBoxButton.setFocus(true);
					}
				  };
				  
				  loginService.loginServer(getTxtBxCorreoElectronico().getText(),getTxtBxPassword().getText(),callback);
							
			}
		});
		
		
		getBtnRegistro().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
//				setVisibilidad(false);
			
				rootPanel.remove(rootPanel.getWidget(1));
				rootPanel.add(new RegistrarUsuario(),100,50);
			
			}
		});
		
		getBtnVerCompetenciasDeportivas().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
							
//				setVisibilidad(false);
				rootPanel.remove(rootPanel.getWidget(1));
				rootPanel.add(new ListarCompetenciasDeportivasUNA(),125,65);
			}
		});
		
		
		
		// TAB INDEX
		
		getTxtBxCorreoElectronico().setTabIndex(1);
		getTxtBxPassword().setTabIndex(2);
		getBtnOk().setTabIndex(3);
		getBtnVerCompetenciasDeportivas().setTabIndex(4);
		getBtnRegistro().setTabIndex(5);
		
		
	}
	
	public Button getBtnOk() {
		return btnOkIngreso;
	}
	public Button getBtnVerCompetenciasDeportivas() {
		return btnVerCompetenciasDeportivas;
	}
	public TextBox getTxtBxCorreoElectronico() {
		return txtBxCorreoElectronico;
	}
	public Button getBtnRegistro() {
		return btnRegistro;
	}
	public PasswordTextBox getTxtBxPassword() {
		return txtBxPassword;
	}
	
	public void setVisibilidad(boolean estado){
		this.setVisible(estado);
	}
	
}
